home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / 8bit / cislib_b / pasexp.doc < prev    next >
Text File  |  1995-04-22  |  2KB  |  52 lines

  1. EXPONENTIATION IN PASCAL
  2.  
  3. A common question asked to Kyan is "How do I raise a number to a 
  4. power?"  Standard Pascal provides no means of doing this, which is 
  5. usually implemented with a statement such as:
  6.  
  7.         POWER=BASE^NUM:REM BASIC
  8.  
  9. or...
  10.  
  11.         Power := Base ** Num;  (* Some implementations of Pascal *)
  12.  
  13. Fortunately, there is a short and simple routine that can do it in 
  14. Standard Pascal.  Its main statement is:
  15.  
  16.         Power := Exp(Num * Ln(Base));
  17.  
  18. (You will recall that Exp and Ln are predefined, Standard Pascal 
  19. functions.)  You can call this function with two real numbers; the 
  20. first number is the base, and the second is the number you wish to 
  21. raise the base to.  Here's an example of calling Power:
  22.  
  23.         NewNum := Power(Base,Raiser);
  24.  
  25. That is the 'quick & dirty' method of raising a number to a power.  
  26. 'Quick' applies only to the fact that it is one line long; this method 
  27. of exponentiation is not very efficient and may take too long to do 
  28. the calculation.  'Dirty' applies to the limitation of not accepting 
  29. negative numbers and the fact that round-off errors may result.  For 
  30. casual programming problems, this routine is acceptable, but for 
  31. more advanced problems which require speed, efficiency, and 
  32. accuracy, the longer version of Power must be used.
  33.  
  34. This longer Power routine is based on one written by Jon Snader 
  35. (who has a Ph.D in mathematics) which appeared in the September 
  36. 1986 issue of Computer Language magazine.  Before you use this 
  37. longer version of Power, make sure that you have included the file 
  38. FRAC.I, which is available in the data library under the filename 
  39. FRAC.PAS--see FRAC.DOC, too.  This Power is called just like the 
  40. 'lite' version of Power:
  41.  
  42.         NewNum := Power(Base,Raiser);
  43.  
  44. NOTE:  Since these function both have the same name, one can not 
  45. include them both in a program (although I don't know why one would 
  46. want to).  When you download them, they are BOTH in the file 
  47. PASEXP.PAS--be sure to separate them with the Kyan Text Editor 
  48. and make sure that they are saved with different filenames on the 
  49. disk.  For the 'lite' version, I recommend PWR.I as a filename, and for 
  50. the 'pro' version, POWER.I.
  51.  
  52.